package com.jk;

import com.jk.model.LogBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class LogJob {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Scheduled(cron = "0,10 * * * * ?") //每10秒调用一次方法
    public void testTasks() {
        long time = new Date().getTime();  //获取当前时间毫秒值
        long time2 = time-(1000*60*60*24*15) ;
        Date date = new Date();
        date.setTime(time2);
        System.out.println(date);
        Query query = new Query();
        query.addCriteria(Criteria.where("createTime").lte(date));
        mongoTemplate.remove(query, LogBean.class);

    }

    //private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //@Scheduled(fixedRate = 1000*60) //每一分钟执行一次
    //@Scheduled(fixedRate = 1000*60*60*24*15) //15天
   /* public void testTasks1() {
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));//打印执行时间
        mongoTemplate.dropCollection(LogBean.class);
        long time = new Date().getTime();
        System.out.println(time);
    }*/

     //每月15号执行
    /*@Scheduled(cron = "0 0 0 15 * ?" )
    public void testTasks() {
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
        mongoTemplate.dropCollection(LogBean.class);
    }*/


}
